package com.daiyang.lottery.service.impl;

import com.daiyang.lottery.controller.param.CreatePrizeParam;
import com.daiyang.lottery.controller.param.PageListParam;
import com.daiyang.lottery.dao.dataobject.PrizeDO;
import com.daiyang.lottery.dao.mapper.PrizeMapper;
import com.daiyang.lottery.service.PictureService;
import com.daiyang.lottery.service.PrizeService;
import com.daiyang.lottery.service.dto.PageListDTO;
import com.daiyang.lottery.service.dto.PrizeDTO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class PrizeServiceImpl
        implements PrizeService {
    private static Logger logger = LoggerFactory.getLogger(PrizeServiceImpl.class);

    @Resource(name = "prizeMapper")
    private PrizeMapper prizeMapper;
    @Resource(name = "pictureServiceImpl")
    private PictureService pictureService;

    @Override
    public Long creteaPrize(CreatePrizeParam createPrizeParam, MultipartFile prizePic) {
        //保存图片
        String filename = pictureService.savePicture(prizePic);


        //保存奖品信息
        PrizeDO prizeDO = new PrizeDO();
        prizeDO.setPrice(createPrizeParam.getPrice());
        prizeDO.setDescription(createPrizeParam.getDescription());
        prizeDO.setImageUrl(filename);
        prizeDO.setName(createPrizeParam.getPrizeName());
        prizeMapper.insert(prizeDO);
        return prizeDO.getId();
    }

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageListParam param) {
        int count=prizeMapper.count();

        List<PrizeDO> prizes =prizeMapper.queryPrizesPage(param.offset(), (param.getPageSize()));
        List<PrizeDTO> records=new ArrayList<>();
        for (PrizeDO prizeDO : prizes) {
            PrizeDTO prizeDTO=new PrizeDTO();
            prizeDTO.setId(prizeDO.getId());
            prizeDTO.setName(prizeDO.getName());
            prizeDTO.setPrice(prizeDO.getPrice());
            prizeDTO.setDescription(prizeDO.getDescription());
            prizeDTO.setImageUrl(prizeDO.getImageUrl());
            records.add(prizeDTO);
        }
        return new PageListDTO<>(count,records);
    }
}
